//
//  AddBlackListView.swift
//  TLCLockDemo
//
//  Created by run on 2024/7/24.
//

import SwiftUI
import TLCLock

struct AddBlackListView: View {


    @State var trackId: String = Server.testTrackId != nil ? "\(Server.testTrackId!)": ""
    @State var begin: Date = .init()
    @State var end: Date = Date().nextYear

    let device: LockDevice

    var body: some View {
        
        VStack {
            YxrInputView("trackId", text: $trackId)
            YxrDatePicker(title: "开始时间", date: $begin)
            YxrDatePicker(title: "结束时间", date: $end)
            
            YxrConfirmView {
                onTapConfirm()
            }
            
            LogView()
            
        }.yxrNavigationTitle("添加黑名单")
    }

    func onTapConfirm() {
        guard let lockKey = Server.getSecret(device.macAddress) else {
            showToast("没有lockKey")
            return
        }

        let trackIdString = trackId.trimmingCharacters(in: .whitespaces)
        guard let tId = UInt32(trackIdString) else {
            showToast("请输入正确的trackId")
            return
        }

        lockTask(desc: "添加黑名单") {
            await device.addBlackList(trackId: tId, begin: begin, end: end, lockKey: lockKey)
        }
    }
}
